package vsqlbuilder.gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

//TODO [Very HIGH]: Validate parameters entered by user

public class ConnectionManager extends JDialog implements ActionListener {

	static final long serialVersionUID = 0L; // TODO [low]: temporary fix, must be
												// change to correct one

	private JButton leftButton, rightButton;
	private JLabel[] labels;
	private JTextField[] textFields;
	private int numParameters;
	private MainController Controller;

	private static final int DIALOG_WIDTH = 480;

	private static final int DIALOG_HEIGHT = 300;

	public ConnectionManager(JFrame owner, MainController Controller) {
		super(owner, true); // Dialog will be modal
		setBackground(Color.WHITE);
		setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setTitle("Connection Data");
		this.setAlwaysOnTop(true);
		this.Controller = Controller;

		numParameters = 5;
		labels = new JLabel[numParameters];
		textFields = new JTextField[numParameters];

		labels[0] = new JLabel("Host Name:");
		labels[1] = new JLabel("Host Port:");
		labels[2] = new JLabel("Database Name:");
		labels[3] = new JLabel("User Name:");
		labels[4] = new JLabel("Password:");

		// Create test fields
		for (int i = 0; i < (numParameters-1); i++) {
			textFields[i] = new JTextField(10);
		}
		textFields[(numParameters-1)] = new JPasswordField (10);

		// For accessibility's sake, associate each label with its text field.
		for (int i = 0; i < numParameters; i++) {
			labels[i].setLabelFor(textFields[i]);
		}
		// Set default value for text fields if defined
		if (this.Controller.Logic.DatabaseConnection != null) {
			textFields[0].setText(this.Controller.Logic.DatabaseConnection
					.getHostName());
			textFields[1].setText(Integer
					.toString(this.Controller.Logic.DatabaseConnection
							.getPort()));
			textFields[2].setText(this.Controller.Logic.DatabaseConnection
					.getDatabaseName());
			textFields[3].setText(this.Controller.Logic.DatabaseConnection
					.getUser());
			textFields[4].setText(this.Controller.Logic.DatabaseConnection
					.getPassword());
		} else
			textFields[1].setText("5432"); // only define default port

		GridLayout layout = new GridLayout(6, 2);

		// Create the container and add the components to it.
		JPanel panel = new JPanel(layout);
		for (int i = 0; i < numParameters; i++) {
			panel.add(labels[i]);
			panel.add(textFields[i]);
		}

		// TODO [medium]: Add image icon to the left & right button
		ImageIcon leftButtonIcon = null;
		leftButton = new JButton("OK", leftButtonIcon);
		leftButton
				.setToolTipText("Click this button to configure database connection with given parameters");
		leftButton.setActionCommand("accept");
		leftButton.addActionListener(this);
		panel.add(leftButton);

		ImageIcon rightButtonIcon = null;
		rightButton = new JButton("Cancel", rightButtonIcon);
		rightButton
				.setToolTipText("Click this button to abort configuration of database connection");
		rightButton.setActionCommand("cancel");
		rightButton.addActionListener(this);
		panel.add(rightButton);

		panel.setLayout(layout);
		this.add(panel);
		setLocationRelativeTo(owner);
		this.pack();
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {

		if ("accept".equals(e.getActionCommand())) {
			Controller.registerConnection(textFields);
		}

		if ("cancel".equals(e.getActionCommand())) {
			// Clear the fields without care if changes were applied or not
			/*for (int i = 0; i < numParameters; i++) {
				if(i!=1){
					textFields[i].setText("");					
				}
			}*/
		}

		this.setVisible(false);

	}

	public void cleanTextFields() {
		// Clear the fields of the form
		for (int i = 0; i < numParameters; i++) {
			textFields[i].setText("");
		}
		textFields[1].setText("5432"); // only define default port
	}

}
